﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Film.Common;
using Film.Repository;

namespace PhuongDoAn.UC
{
    public partial class uc_detail : System.Web.UI.UserControl
    {
        public int movieID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    int.TryParse(Request.QueryString["id"], out movieID);
                }
                if (movieID == 0)
                {
                    Page.Response.Write("Default.aspx");
                }
                try
                {
                    tblMovie entity = new tblMovie();
                    tblMovieRepository movieRepository = new tblMovieRepository();
                    entity = movieRepository.GetByMovieID(movieID);
                    if (entity == null || (entity != null && entity.Status == false))
                    {
                        Page.Response.Write("Default.aspx");
                    }

                    txtMovieName.Text = HttpUtility.HtmlEncode(entity.MovieName);
                    txtDirector.Text = HttpUtility.HtmlEncode(entity.Director);
                    txtCountry.Text = "";

                    if (entity.tblCountry != null)
                    {
                        txtCountry.Text = HttpUtility.HtmlEncode(entity.tblCountry.CountryName);
                    }

                    cbYear.Text = entity.Year.ToString();
                    cbType.Text = entity.Genre;
                    ltActor.Text = "";

                    ltImage.Text = "<img src='" + Util.GetImageUrl(entity.ImageUrl) + "' style='width: 120px' />";

                    #region Load other

                    tblAgeRepository ageRepository = new tblAgeRepository();
                    List<tblAge> listAge = ageRepository.GetAll();

                    tblRatingRepository ratingRepository = new tblRatingRepository();
                    List<tblRating> listRating = ratingRepository.GetRatingByMovieID(entity.MovieID);


                    List<clsDetailAgeRating> result = new List<clsDetailAgeRating>();
                    foreach (tblAge ageEntity in listAge)
                    {
                        clsDetailAgeRating clsEntity = new clsDetailAgeRating();
                        clsEntity.AgeID = ageEntity.AgeID;
                        clsEntity.AgeName = ageEntity.AgeName;

                        listRating = ratingRepository.GetRatingByMovieIDAndAgeID(entity.MovieID, ageEntity.AgeID);
                        clsEntity.SumRating = listRating.Sum(x => x.Rating);
                        clsEntity.TotalRating = listRating.Count();
                        clsEntity.AvgRating = (clsEntity.TotalRating > 0) ? (int)Math.Ceiling(clsEntity.SumRating / clsEntity.TotalRating) : 0;


                        result.Add(clsEntity);
                    }

                    rptRating.DataSource = result;
                    rptRating.DataBind();

                    #endregion Load other


                }
                catch (Exception ex)
                {
                    Page.Response.Write("Default.aspx");
                }
            }
        }
    }

    public class clsDetailAgeRating
    {
        public int AgeID { get; set; }
        public string AgeName { get; set; }
        public int TotalRating { get; set; }
        public double SumRating { get; set; }
        public double AvgRating { get; set; }
    }

}